/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qp.smsmarketing.dao.abstractdao;

import com.qp.smsmarketing.entity.Grupo;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author waldenilson
 */
public abstract class AbstractDAO implements IDAO {

    protected SessionFactory sf;
    protected Session session;
    protected Transaction transaction;
    protected EntityManager entityManager;
    protected Query query;

    // abstract methods
    protected List findAllRows(String entityClass) {
        //if(session == null)
        //  session = sf.openSession();
        //List l = session.createQuery("FROM "+entityClass).list();

        List l = entityManager.createQuery("FROM " + entityClass).getResultList();

        return l;
    }

    /**
     * Lembrar de refatorar codigo do metodo
     *
     * @param entityClass
     * @param data
     * @return
     */
    protected List findAllRowsPorData(String entityClass, Date data) {

        List l = null;
        
        query = entityManager.createQuery("SELECT e FROM " + entityClass
                + " e WHERE e.data=:tt");
        query.setParameter("tt", data);

        if (!query.getResultList().isEmpty()) {
            l = query.getResultList();
        }


        return l;
    }

    /**
     * Lembrar de refatorar codigo do metodo
     *
     * @param entityClass
     * @param data
     * @return
     */
    protected Object findAllRowsRestricao(String entityClass, Object valor, String coluna) {
        Object l = null;
        try {

            query = entityManager.createQuery("SELECT " + coluna + " FROM " + entityClass
                    + " e WHERE e.titulo=:tt");
            query.setParameter("tt", valor.toString());

            l = query.getSingleResult();
        } catch (NullPointerException e) {
        }
        return l;
    }

    protected Object findOne(String entityClass, int id) {

        query = entityManager.createQuery("SELECT e FROM " + entityClass
                + " e WHERE e.id" + entityClass + " = :id");

        query.setParameter("id", id);

        Object o = query.getSingleResult();

        return o;
    }

    protected List findAllRowsActive(String entityClass) {
        query = entityManager.createQuery("SELECT e FROM " + entityClass
                + " e WHERE e.status" + entityClass + " = :status");

        query.setParameter("status", true);
        List l = query.getResultList();

        return l;
    }

    protected boolean salvar(Object o) {
        try {
//            if(session == null)
//                session = sf.openSession();
//            session.save(o);
//            transaction = session.beginTransaction();
//            transaction.commit();
//            session.close();
            entityManager.getTransaction().begin();
            entityManager.persist(o);
            entityManager.getTransaction().commit();


            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    protected boolean atualizar(Object o) {
        try {
//           if(session == null)
//                session = sf.openSession();
//            transaction = session.beginTransaction();
//            session.merge(o);
//            transaction.commit();
//            session.close();

            entityManager.getTransaction().begin();
            entityManager.merge(o);
            entityManager.getTransaction().commit();

            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    /**
     * @return falso se não for possivel realizar exclusão.
     */
    protected boolean excluir(Object o) {

        try {
            entityManager.getTransaction().begin();
//            entityManager.
            entityManager.getTransaction().commit();
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }

    }
}
